//
//  ViewController.m
//  PinchScale
//
//  Created by mac on 2017/4/2.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ViewController.h"
#import "UIViewController+DragAndPinchScale.h"
#import "WorkTime.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight  [UIScreen mainScreen].bounds.size.height
#define kMaxScale 4
#define kMinScale 1

@interface ViewController ()

@property (nonatomic, strong) UIScrollView *bgdScrollView;
@property (nonatomic, strong) NSMutableArray *widthArray;
@property (nonatomic, assign) CGFloat currentScale;
@property (nonatomic, assign) CGFloat changedScale;
@property (nonatomic, assign) BOOL isAllowedToChangeFrame;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.widthArray = [[NSMutableArray alloc]initWithObjects:@"200",@"200",@"300",@"400",@"700",@"300",@"200",@"200", nil];
    
    UIScrollView *bgd = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    bgd.contentSize = CGSizeMake(kScreenWidth, kScreenHeight);
    [bgd setBackgroundColor:[UIColor whiteColor]];
    self.bgdScrollView = bgd;
    [self.view addSubview:self.bgdScrollView];
    
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlerPinchGesture:)];
    self.currentScale = 1.0;
    self.changedScale = 1.0;
    self.isAllowedToChangeFrame = YES;
    [self.view addGestureRecognizer:pinchGesture];
    
    __block float startPointX = 0;
    [self.widthArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *str = [self.widthArray objectAtIndex:idx];
        float width = [str floatValue];
        width *= self.currentScale;
        WorkTime *workTime = [[NSBundle mainBundle]loadNibNamed:@"WorkTime" owner:self options:nil].firstObject;
        [workTime setFrame:CGRectMake(startPointX, 0, width, 100)];
        [self.bgdScrollView addSubview:workTime];
        startPointX += width;
    }];
    CGSize size = self.bgdScrollView.contentSize;
    size.width = startPointX;
    self.bgdScrollView.contentSize = size;
    
}

- (void)changeWorkTimeView{
    
    //[self.bgdScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //__weak typeof(self) weakself = self;
    NSArray *subviews = self.bgdScrollView.subviews;
    
    //改变后的contentSize
    CGSize size = self.bgdScrollView.contentSize;
    size.width *= self.changedScale;
    self.bgdScrollView.contentSize = size;
    
    [subviews enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        __block UIView *view = [subviews objectAtIndex:idx];
        if ([view isKindOfClass:[WorkTime class]]) {
            __block float width = view.frame.size.width;
            __block float originX = view.frame.origin.x;
            width *= self.changedScale;
            originX *= self.changedScale;
            [view setFrame:CGRectMake(originX, 0, width, 100)];
//            [UIView animateWithDuration:0.3 animations:^{
//                self.isAllowedToChangeFrame = NO;
//                //self.changedScale = 1.0;
//                [view setFrame:CGRectMake(originX, 0, width, 100)];
//            } completion:^(BOOL finished) {
//                weakself.isAllowedToChangeFrame = YES;
//            }];
        }
        
    }];

}
- (void)handlerPinchGesture:(UIPinchGestureRecognizer *)pinch{
    
    NSLog(@"pinch.scale ==%f",pinch.scale);
    NSLog(@"self.currentScale== %f",self.currentScale);
    
    CGFloat scale = pinch.scale;
    self.changedScale = scale;
    //缩放情况
    if(scale < 1.0){
        if (self.currentScale < kMinScale) return;
    }
    
    //放大情况
    if (scale > 1.0) {
        if (self.currentScale >= kMaxScale) return;
    }
    
    self.currentScale *= self.changedScale;
    [self changeWorkTimeView];
    pinch.scale = 1.0;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
